package pers.xhh.frontapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.xhh.frontapi.common.Mi;
import pers.xhh.frontapi.common.ResultCode;
import pers.xhh.frontapi.dao.MiReceiverDao;
import pers.xhh.frontapi.entity.MiReceiverEntity;
import pers.xhh.frontapi.entity.MiUserEntity;
import pers.xhh.frontapi.entity.to.receiver.MiReceiverPutTo;
import pers.xhh.frontapi.entity.to.receiver.MiReceiverTo;
import pers.xhh.frontapi.interceptor.AuthenticationInterceptor;
import pers.xhh.frontapi.service.MiReceiverService;

import java.util.Date;
import java.util.List;

/**
 * @author wei-xhh
 * @date 2020-11-22
 */
@Service
public class MiReceiverServiceImpl extends ServiceImpl<MiReceiverDao, MiReceiverEntity> implements MiReceiverService {

    @Override
    public Mi addReceiver(MiReceiverTo miReceiverTo) {
        // 获取登录后的信息
        MiUserEntity miUserEntity =
                AuthenticationInterceptor.threadLocal.get();

        MiReceiverEntity miReceiverEntity = new MiReceiverEntity();
        miReceiverEntity.setCreateTime(new Date());
        miReceiverEntity.setUpdateTime(new Date());
        miReceiverEntity.setReceiverName(miReceiverTo.getReceiverName());
        miReceiverEntity.setReceiverDetailAddress(miReceiverTo.getReceiverDetailAddress());
        miReceiverEntity.setReceiverPhone(miReceiverTo.getReceiverPhone());
        miReceiverEntity.setReceiverPostCode(miReceiverTo.getReceiverPostCode());
        miReceiverEntity.setReceiverProvince(miReceiverTo.getReceiverProvince());
        miReceiverEntity.setReceiverCity(miReceiverTo.getReceiverCity());
        miReceiverEntity.setReceiverRegion(miReceiverTo.getReceiverRegion());
        miReceiverEntity.setUserId(miUserEntity.getUserId());

        boolean save = this.save(miReceiverEntity);
        if (save) {
            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), "保存成功！");
        }
        return Mi.error(ResultCode.ERROR1100.getMessage(), ResultCode.ERROR1100.getStatus());
    }

    @Override
    public Mi updateReceiver(Integer receiverId, MiReceiverPutTo miReceiverTo) {
        if (miReceiverTo != null) {
            String receiverCity = miReceiverTo.getReceiverCity();
            String receiverDetailAddress = miReceiverTo.getReceiverDetailAddress();
            String receiverName = miReceiverTo.getReceiverName();
            String receiverPhone = miReceiverTo.getReceiverPhone();
            String receiverPostCode = miReceiverTo.getReceiverPostCode();
            String receiverProvince = miReceiverTo.getReceiverProvince();
            String receiverRegion = miReceiverTo.getReceiverRegion();

            MiReceiverEntity miReceiverEntity = new MiReceiverEntity();
            miReceiverEntity.setId(receiverId);
            if (receiverCity != null) {
                miReceiverEntity.setReceiverCity(receiverCity);
            }
            if (receiverDetailAddress != null) {
                miReceiverEntity.setReceiverDetailAddress(receiverDetailAddress);
            }
            if (receiverName != null) {
                miReceiverEntity.setReceiverName(receiverName);
            }
            if (receiverPhone != null) {
                miReceiverEntity.setReceiverPhone(receiverPhone);
            }
            if (receiverPostCode != null) {
                miReceiverEntity.setReceiverPostCode(receiverPostCode);
            }
            if (receiverProvince != null) {
                miReceiverEntity.setReceiverProvince(receiverProvince);
            }
            if (receiverRegion != null) {
                miReceiverEntity.setReceiverRegion(receiverRegion);
            }

            // ->更新
            boolean b = this.updateById(miReceiverEntity);
            if (b) {
                return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), "更新成功!");
            }
            return Mi.error(ResultCode.ERROR1101.getMessage(), ResultCode.ERROR1101.getStatus());
        }
        return Mi.ok(ResultCode.ERROR1101.getMessage(), ResultCode.ERROR1101.getStatus(), "更新地址参数为空！");

    }

    @Override
    public Mi deleteReceiver(Integer receiverId) {
        if (receiverId == null || receiverId <= 0) {
            return Mi.error(ResultCode.ERROR1102.getMessage(), ResultCode.ERROR1102.getStatus());
        }
        boolean b = this.removeById(receiverId);
        if (b) {
            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), "删除成功！");
        }
        return Mi.error(ResultCode.ERROR1103.getMessage(), ResultCode.ERROR1103.getStatus());
    }

    @Override
    public Mi getAllReceiver() {
        // 获取登录后的信息
        MiUserEntity miUserEntity =
                AuthenticationInterceptor.threadLocal.get();

        List<MiReceiverEntity> miReceiverEntities =
                this.list(new QueryWrapper<MiReceiverEntity>().eq("user_id", miUserEntity.getUserId()));

        return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), miReceiverEntities);
    }
}
